from exts import mysql
import pymysql
import re
# 搜索特价

def redefine(str):#处理所有+号的变量
    if str==None:
        return ['']
    resultlist = str.split('+')
    tag=0
    for count in range(len(resultlist)):
        if resultlist[count]=='':
            tag=True
        resultlist[count] = resultlist[count].strip()
    if tag:
        resultlist.remove('')
    return resultlist

def searchscore(hid):#评分
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `scores` WHERE scores.hid="+hid)
    t=cursor.fetchall()
    result=[]
    for i in t:
        resultdict = dict(hid=i[0], total=i[3], tcomment=i[4], suzhi=i[5], sheshifuwu=i[7],qingjiechengdu=i[9],shushichengdu=i[11],xingjiabi=i[13],weizhi=i[15],mianfeiwifi=i[17])
        result.append(resultdict)
    #print(result)

    return result

def displayrooms(hid,people):#房间
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM`rooms` WHERE hotel_num="+hid+" GROUP BY room_name")
    t = cursor.fetchall()
    result = []
    for i in t:
        temp_string=i[9]
        numbers = [int(temp) for temp in temp_string.split() if temp.isdigit()]
        resultdict = dict(hid=i[0],hotelname=i[2],roomname=i[3], num=i[4],bedtype=i[5], maxpeople=numbers[0],price=i[10],roomfeature=i[6],roomfacility=redefine(i[7]),roomfacilities=redefine(i[8]),roompriceother=redefine(i[11]),roomsbreakfast=redefine(i[12]),roomcancel=redefine(i[13])[0],pay=redefine(i[14]),detail=redefine(i[15]),roomid=i[16])
        #print(resultdict)
        result.append(resultdict)
    #print(result)
    return result


def displayrestaurant(hid):#餐厅
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `restaurant` WHERE restaurant.hid="+hid+" GROUP BY describes")
    t = cursor.fetchall()
    result = []
    for i in t:
        resultdict = dict(hid=i[0], restaurantname=i[3], flavor=i[4], time=i[5],type=i[6])
        result.append(resultdict)

    print(result)
    return result

def displaypolicy(hid):#政策
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `policy` WHERE policy.hid="+hid)
    t = cursor.fetchall()
    result = []
    for i in t:
        resultdict = dict(hid=i[0], checkin=i[3], checkout=i[4],cancel=i[5], deposit=i[6],child=redefine(i[7]),add=redefine(i[8]),age=redefine(i[9]),pet=redefine(i[10]),team=redefine(i[11]),shouldknow=redefine(i[12]))
        result.append(resultdict)
    #print(result)
    return result

def displayinfo(hid):#基础信息
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `hotelinfo` WHERE hid="+hid)

    t = cursor.fetchall()
    result = []
    for i in t:
        resultdict = dict(hid=i[0],hname=i[2],address=i[6], point=i[7],photonum=i[8],star=i[9],htype=i[3])
        result.append(resultdict)
    #print(result)
    return result


def displaydescribe(hid):#基础信息
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `hoteldescribes` WHERE hid="+hid)
    t = cursor.fetchall()
    result = []
    for i in t:
        resultdict = dict(hid=i[0],f1=redefine(i[3]),des=i[4], brand=i[6],f2=redefine(i[7]))
        result.append(resultdict)
    #print(result)
    return result


def displayfac(hid):#设施
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM `facilities` WHERE hid="+hid)
    t = cursor.fetchall()
    result = []
    for i in t:
        resultdict = dict(hid=i[0],detail=redefine(i[3]))
        result.append(resultdict)
    #print(result[0])
    return result[0]

if __name__ == '__main__':

    #searchscore('1')
    a=displayrooms('500',1)
    print(a)
    #displayrestaurant('1')
    #displaypolicy('500')
    #displayinfo('500')
    #displaydescribe('500')
    #displayfac('500')